package com.kzd.gtboon.jetpack.api

import com.blankj.utilcode.util.AppUtils
import com.kzd.gtboon.app.*
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.utils.*
import com.module.lemlin.http.CustomException
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.delay
import rxhttp.toClass

/**
 * 验证码登录
 * */
suspend fun appLoginVerify(
    phone: String, code: String, sessionid: String,
): HttpResponseDataT<AccessUser> {
    val uidResponse = RxHttp.postForm(APP_LOGIN_PHONE)
        .addAll(httpSign(phone, code, sessionid, key = key2))
        .add("phone", phone)
        .add("code", code)
        .add("sessionid", sessionid)
        .toClass<HttpResponseDataT<String>>()
        .await()
    if (uidResponse.code != 1) {
        throw CustomException(uidResponse.code.toString(), uidResponse.message)
    }
    val userInfoResponse = RxHttp.postForm(USER_GET_USER_INFO)
        .setAssemblyEnabled(false)
        .add("uid", uidResponse.data)
        .toClass<HttpResponseDataT<UserInfo>>()
        .await()
    val userMineResponse = RxHttp.postForm(USER_MY_INFO)
        .setAssemblyEnabled(false)
        .add("uid", uidResponse.data)
        .toClass<HttpResponseDataT<UserMine>>()
        .await()
    val accessUser = AccessUser(
        uid = uidResponse.data,
        userInfo = userInfoResponse.data,
        userMine = userMineResponse.data
    )
    return HttpResponseDataT(accessUser).apply {
        this.code = uidResponse.code
        this.message = uidResponse.message
    }
}


/**
 * 用户一键登录
 * */
suspend fun appLoginOneKey(token: String): HttpResponseDataT<AccessUser> {
    val uidResponse = RxHttp.postForm(APP_LOGIN_ONE_KEY)
        .addAll(httpSign(token, key = key2))
        .add("token", token)
        .toClass<HttpResponseDataT<String>>()
        .await()
    if (uidResponse.code != 1) {
        throw CustomException(uidResponse.code.toString(), uidResponse.message)
    }
    val userInfoResponse = RxHttp.postForm(USER_GET_USER_INFO)
        .setAssemblyEnabled(false)
        .add("uid", uidResponse.data)
        .toClass<HttpResponseDataT<UserInfo>>()
        .await()
    val userMineResponse = RxHttp.postForm(USER_MY_INFO)
        .setAssemblyEnabled(false)
        .add("uid", uidResponse.data)
        .toClass<HttpResponseDataT<UserMine>>()
        .await()
    val accessUser = AccessUser(
        uid = uidResponse.data,
        userInfo = userInfoResponse.data,
        userMine = userMineResponse.data
    )
    return HttpResponseDataT(accessUser).apply {
        code = uidResponse.code
        message = uidResponse.message
    }
}

/**
 * 游戏分类tab
 * */
suspend fun appClassify(): HttpResponseDataE<ClassifyTab> =
    RxHttp.postForm(APP_FENLEI)
        .toClass<HttpResponseDataE<ClassifyTab>>()
        .await()

/**
 * 游戏分类列表
 *
 * @param type 分类id
 * @param page 分页
 * */
suspend fun appClassifyGame(
    type: String, page: String,
): HttpResponseDataE<GameBean> = RxHttp.postForm(APP_FENLEI_GAME)
    .add("type", type)
    .add("p", page)
    .toClass<HttpResponseDataE<GameBean>>()
    .await()

/**
 * 游戏排行
 *
 * @param type 0热门榜1新游榜
 * @param cid 分类id
 * @param page 分页
 * */
suspend fun appRanking(
    type: String, cid: String, page: String,
): HttpResponseDataE<GameBean> = RxHttp.postForm(APP_PAIHANG)
    .add("t", type)
    .add("type", cid)
    .add("p", page)
    .toClass<HttpResponseDataE<GameBean>>()
    .await()

/**
 * 热门搜索
 * */
suspend fun appHotSearch(): HttpResponseDataE<SearchHot> =
    RxHttp.postForm(APP_HOT_SO)
        .toClass<HttpResponseDataE<SearchHot>>()
        .await()

/**
 * 搜索结果信息
 *
 * @param keyword
 * */
suspend fun appSearchInfo(keyword: String): HttpResponseDataE<GameBean> =
    RxHttp.postForm(APP_SEARCH_INFO)
        .add("keyword", keyword)
        .toClass<HttpResponseDataE<GameBean>>()
        .await()

/**
 * 礼包领取
 * */
suspend fun appGetGift(lid: String): WelfareGiftGetResponse =
    RxHttp.postForm(APP_GET_LB)
        .addAll(httpSign(SPSConstance.uid, key = key1))
        .add("id", lid)
        .toClass<WelfareGiftGetResponse>()
        .await()

/**
 * 检测更新
 * */
suspend fun appIsUpDate(delay: Long = 0,id: String = "1"): HttpResponseDataT<AppUpdate> =
    RxHttp.get(APP_IS_UPDATE)
        .add("versionCode", AppUtils.getAppVersionCode().toLong())
        .add("id",id)
        .toClass<HttpResponseDataT<AppUpdate>>()
        .delay(delay)
        .await()

/**
 * 游戏详情
 *
 * @param gid 游戏id
 * */
suspend fun appGameInfo(gid: String): HttpResponseDataT<GameDetails> =
    RxHttp.postForm(APP_GAME_INFO)
        .add("id", gid)
        .toClass<HttpResponseDataT<GameDetails>>()
        .await()